﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace SatisAktarim
{
    public static class Cnv
    {
        public static T Parse<T>(string sourceValue) where T : IConvertible
        {
            if (string.IsNullOrEmpty(sourceValue))
                return (T)DefaultSetting(typeof(T).ToString());
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }

        public static T Parse<T>(object sourceValue) where T : IConvertible
        {
            if (sourceValue == null || sourceValue == DBNull.Value)
                return (T)DefaultSetting(typeof(T).ToString());
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(object sourceValue, object defaultValue) where T : IConvertible
        {
            if (sourceValue == null || sourceValue == DBNull.Value)
                return (T)DefaultSetting(typeof(T).ToString());
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), null);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), null);
            }
        }

        public static T Parse<T>(string sourceValue, string defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(Int16 sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Int16 sourceValue, Int16 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(Int32 sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Int32 sourceValue, Int32 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(Int64 sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Int64 sourceValue, Int64 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(DateTime sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(DateTime sourceValue, Int64 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(Boolean sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Boolean sourceValue, Boolean defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(decimal sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(decimal sourceValue, decimal defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(float sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(float sourceValue, float defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static T Parse<T>(byte sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(byte sourceValue, byte defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T));
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
        }

        public static object DefaultSetting(string type)
        {
            switch (type)
            {
                case "System.String":
                    return string.Empty;
                case "System.Int32":
                    return 0;
                case "System.Int16":
                    return Convert.ToInt16(0);
                case "System.Int64":
                    return Convert.ToInt64(0);
                case "System.DateTime":
                    return DateTime.Now;
                case "System.Decimal":
                    return Convert.ToDecimal(0);
                case "System.Boolean":
                    return false;
                case "System.Double":
                    return Convert.ToDouble(0);
                case "System.Single":
                    return Convert.ToSingle(0);
                case "System.Byte":
                    return Convert.ToByte(0);
                default:
                    return null;
            }

        }

        public static DateTime ToDateTime(this string tarih)
        {
            return new DateTime(Parse<int>(tarih.Substring(6, 4)), Parse<int>(tarih.Substring(3, 2)), Parse<int>(tarih.Substring(0, 2)));
        }

        public static int ToInt(this string deger)
        {
            return Parse<int>(deger);
        }

        public static byte ToByte(this string deger)
        {
            return Parse<byte>(deger);
        }

        public static bool ToBool(this string deger)
        {
            return Parse<bool>(deger);
        }

        public static decimal ToDecimal(this string deger)
        {
            return Parse<decimal>(deger);
        }

        public static string GenerateClosureID(this Document doc)
        {
            return string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", doc.PosNo.ToString("000"), doc.Tarih.Day.ToString("00"), doc.Tarih.Month.ToString("00"), doc.Tarih.Year.ToString().Substring(2, 2), doc.ID.ToString("0000"), doc.Saat, doc.MagazaNo.ToString("000"), doc.BelgeTipi.ToString());
        }

        public static Byte[] Serialize(Document evrak)
        {
            MemoryStream mem = new MemoryStream();
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(mem, evrak);
            mem.Close();
            return mem.ToArray();
        }

        public static Document DeSerialize(Byte[] data)
        {
            Document evr = null;
            MemoryStream mem = new MemoryStream(data);
            BinaryFormatter bformatter = new BinaryFormatter();
            evr = (Document)bformatter.Deserialize(mem);
            mem.Close();
            return evr;
        }
    }
}
